package com.voipAgent;

import java.util.HashMap;

import com.cvtt.core.SIPCore;
import com.cvtt.sip.SIPConfig;
import com.cvtt.sip.SIPEngine;
import com.zoolu.sip.provider.SipProvider;

import android.content.Context;

public class SipAgent {
	public static final int U_START = 100;
	public static final int U_STOP = 101;
	public static final int U_RESTART = 102;
	public static final int U_LOGOUT = 103;
	public static final int U_AWAY = 104;
	public static final int U_RELOGIN = 105;
	public static final int U_LOGIN_SUCCESS = 106;
	public static final int U_LOGIN_FAIL = 107;
	public static final int U_STATUS_CHANGE = 108;
	public static final int U_KICK = 109;
	public static final int U_SIP_KEEP_ALIVE = 110;
	public static final int U_SIP_LOGIN = 112;

	public static final int U_CALL_OUT = 113;// 发起外呼
	public static final int U_CALL_IN = 114;// 收到呼叫
	public static final int U_END_CALL = 115;// 主动结束通话
	public static final int U_CALL_END = 116;// 被动结束通话
	public static final int U_SEND_DTMF = 117;// 发送DTMF
	public static final int U_CALL_OK = 118;// 通话建立
	public static final int U_CALL_RING = 119;
	/** 在程序退出或重置密码时，先检查是否有呼叫进来，如果有，将其挂断 */
	public static final int U_CHECK_AND_END_CALL = 120;
	public static final int U_EXIT = 121;
	
	public static final int U_ENTER_BACKGROUND = 122;
	public static final int U_ENTER_FOREGROUND = 123;
	public static final int U_CHECK_SIP_LOGIN = 124;
	
	public static final int U_UPDATE_SIP_STATUS = 125;
	
	public static final String ACTION_CALL_IN = "voip.call.in";
	public static final String EXTRA_CALL_NUMBER = "call_number";
	
	private static SipAgent sipAgent;
	private static Context mContext;
	private SIPCore sipCore;
	
	public static SipAgent getInstance(Context context,SipEventListener eventListener){
		if(sipAgent==null){
			sipAgent=new SipAgent(context,eventListener);
		}
		mContext=context;
		return sipAgent;
	}
	
	private SipAgent(Context context,SipEventListener sipEventListener){		
		sipCore=SIPCore.getInstance(context,sipEventListener);
	}
	public void setConfig(String sipServer,String sipDomain,int sipPort,String sipSlaveServer,String sipSlaveDomain,int sipSlavePort){
		SIPConfig.MASTER_SERVER = sipServer;
		SIPConfig.MASTER_SERVERDOMAIN = sipDomain;
		SIPConfig.MASTER_SERVERPORT = sipPort;
		SIPConfig.SLAVE_SERVER = sipSlaveServer;
		SIPConfig.SLAVE_SERVERDOMAIN = sipSlaveDomain;
		SIPConfig.SLAVE_SERVERPORT = sipSlavePort;
	}
	public void start(){
		/*HashMap<String, String> data = new HashMap<String, String>();
		data.put(SIPCore.SIP_MASTER_SERVER,sipServer);
		data.put(SIPCore.SIP_MASTER_SERVERDOMAIN, sipDomain);
		data.put(SIPCore.SIP_MASTER_SERVERPORT, sipPort);*/
		sipCore.postCoreEvent(SipAgent.U_START, null);
	}
	public void reStart(){
		sipCore.postCoreEvent(SipAgent.U_RESTART, null);
	}

	
	public void login(String userName,String password){
		HashMap<String, String> data = new HashMap<String, String>();
		data.put(SIPCore.SIP_USER_NAME, userName);
		data.put(SIPCore.SIP_USER_PASSWORD, password);
		
		sipCore.postCoreEvent(SipAgent.U_SIP_LOGIN, data);
		
	}
	public void reLogin(){
		sipCore.postCoreEvent(SipAgent.U_RELOGIN, null);
	}
	
	public void logout(){
		sipCore.postCoreEvent(SipAgent.U_LOGOUT, null);
	}
	public void callOut(String number){
		sipCore.postCoreEvent(SipAgent.U_CALL_OUT, number);
	}
	
	public void answerCall(){
		sipCore.postCoreEvent(SipAgent.U_CALL_OK, null);
	}
	
	public void endCall(){
		sipCore.postCoreEvent(SipAgent.U_END_CALL , null);
	}
	public void sendDTMF(String data){
		sipCore.postCoreEvent(SipAgent.U_SEND_DTMF , data);
	}
	public SIPEngine getSipProvider(){
		return sipCore.getengine();
		
	}
	
	public void enterBackground(){
		sipCore.postCoreEvent(SipAgent.U_ENTER_BACKGROUND, null);
	}
	
	public void enterForeground(){
		sipCore.postCoreEvent(SipAgent.U_ENTER_FOREGROUND, null);
	}
	
	public void edit(){
		sipCore.postCoreEvent(SipAgent.U_EXIT, null);
	}
	
	public boolean isSipOnline(){
		return sipCore.isOnline();
	}
	
	public interface SipEventListener{
		public void onSipLoginSuccess();
		public void onSipLoginError(int errorcode);
		public void onSipCallRing();
		public void onSipCallOK();
		public void onSipCallEnd(int code);
		public void onSipKick();
		public void isSipOnline(boolean isOnline);
		public void onSipUpdateStatus();
	}
}
